package com.atguigu.daijia.rules.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.daijia.model.entity.rule.FeeRule;
import com.atguigu.daijia.model.form.rules.FeeRuleRequest;
import com.atguigu.daijia.model.form.rules.FeeRuleRequestForm;
import com.atguigu.daijia.model.vo.rules.FeeRuleResponse;
import com.atguigu.daijia.model.vo.rules.FeeRuleResponseVo;
import com.atguigu.daijia.rules.config.DroolsHelper;
import com.atguigu.daijia.rules.mapper.FeeRuleMapper;
import com.atguigu.daijia.rules.service.FeeRuleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
@RequiredArgsConstructor
public class FeeRuleServiceImpl implements FeeRuleService {

    private final KieContainer kieContainer;
    private final FeeRuleMapper feeRuleMapper;

    /**
     * 计算订单费用
     * @param feeRuleRequestForm
     * @return
     */
    /*@Override
    public FeeRuleResponseVo calculateOrderFee(FeeRuleRequestForm feeRuleRequestForm) {
        FeeRuleRequest feeRuleRequest = new FeeRuleRequest();
        BeanUtils.copyProperties(feeRuleRequestForm, feeRuleRequest);
        feeRuleRequest.setStartTime(new DateTime(feeRuleRequestForm.getStartTime()).toString("HH:mm:ss"));
        log.info("规则引擎传入数据:{}", JSON.toJSONString(feeRuleRequest));

        // 获取规则引擎会话
        KieSession kieSession = kieContainer.newKieSession();
        // 设置全局变量，用于规则引擎计算后，返回结果
        FeeRuleResponse feeRuleResponse = new FeeRuleResponse();
        kieSession.setGlobal("feeRuleResponse", feeRuleResponse);
        // 将传入数据插入规则引擎
        kieSession.insert(feeRuleRequest);
        // 执行规则
        kieSession.fireAllRules();
        // 关闭会话
        kieSession.dispose();
        log.info("规则引擎返回数据:{}", JSON.toJSONString(feeRuleResponse));

        // 封装返回结果
        FeeRuleResponseVo feeRuleResponseVo = new FeeRuleResponseVo();
        BeanUtils.copyProperties(feeRuleResponse, feeRuleResponseVo);
        return feeRuleResponseVo;
    }*/

    @Override
    public FeeRuleResponseVo calculateOrderFee(FeeRuleRequestForm feeRuleRequestForm) {
        FeeRuleRequest feeRuleRequest = new FeeRuleRequest();
        BeanUtils.copyProperties(feeRuleRequestForm, feeRuleRequest);
        feeRuleRequest.setStartTime(new DateTime(feeRuleRequestForm.getStartTime()).toString("HH:mm:ss"));
        log.info("规则引擎传入数据:{}", JSON.toJSONString(feeRuleRequest));

        // 从数据库获取最新订单费用规则
        FeeRule feeRule = feeRuleMapper.selectOne(new LambdaQueryWrapper<FeeRule>().orderByDesc(FeeRule::getId).last("limit 1").eq(FeeRule::getStatus, 1));

        // 获取规则引擎会话
        KieSession kieSession = DroolsHelper.loadForRule(feeRule.getRule());

        // 设置全局变量，用于规则引擎计算后，返回结果
        FeeRuleResponse feeRuleResponse = new FeeRuleResponse();
        kieSession.setGlobal("feeRuleResponse", feeRuleResponse);
        // 将传入数据插入规则引擎
        kieSession.insert(feeRuleRequest);
        // 执行规则
        kieSession.fireAllRules();
        // 关闭会话
        kieSession.dispose();
        log.info("规则引擎返回数据:{}", JSON.toJSONString(feeRuleResponse));

        // 封装返回结果
        FeeRuleResponseVo feeRuleResponseVo = new FeeRuleResponseVo();
        BeanUtils.copyProperties(feeRuleResponse, feeRuleResponseVo);
        feeRuleResponseVo.setFeeRuleId(feeRule.getId());
        return feeRuleResponseVo;
    }
}
